/*
 * @Description: 异步并行钩子
 * @Author: wangqi
 * @Date: 2021-04-17 11:59:14
 * @LastEditTime: 2021-04-18 12:14:46
 */
/**
 * 该钩子 tapPromise 和 tapAsync 本质都差不多; 不过这里使用 promise处理
 */

let { AsyncParallelHook, AsyncParallelBailHook } = require('tapable');

class Lesson {
    constructor() {
        this.hook = {
            arch: new AsyncParallelHook(['name']),
        }
    }

    tap() {
        this.hook.arch.tapPromise('node', (name) => {
            return new Promise((resolve, reject) => {
                setTimeout(() => {
                    console.log("node", name);
                    reject();
                }, 1000);
            })

        });

        this.hook.arch.tapPromise('vue', (name) => {
            return new Promise((resolve, reject) => {
                setTimeout(() => {
                    console.log("vue", name);
                    resolve();
                }, 1000);
            })
        });
    }

    start() {
        // 等待上边所有的promise都执行完, 返回一个新的promise
        this.hook.arch.promise('wq1').then(function () {
            console.log('end')
        })
    }
}

let lesson = new Lesson();

lesson.tap();
lesson.start();
